package lc;

import javax.swing.table.TableRowSorter;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试forname与loadclass对于jdbc的区别
 * https://www.cnblogs.com/silyvin/articles/10293512.html
 * Created by sunyuming on 19/1/19.
 */
public class JdbcUtil {

    private static final String URL="jdbc:mysql://192.168.57.67:3306/fast?useUnicode=true&amp;characterEncoding=utf-8";
    private static final String USER="fast";
    private static final String PASSWORD="1qaz@WSX";

    public static void main(String[] args) throws SQLException {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        List<Map<String,Object>> list = new ArrayList<>();
        try{
        //    Class.forName("com.mysql.jdbc.Driver");
            Thread.currentThread().getContextClassLoader().loadClass("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
            st = conn.createStatement();

            PreparedStatement pstmt = conn.prepareStatement("select * from ssd_rs_wx_enterprise_record");
            rs = pstmt.executeQuery();
            ResultSetMetaData md = rs.getMetaData(); //获得结果集结构信息,元数据
            int columnCount = md.getColumnCount();   //获得列数

            while (rs.next()) {
                Map<String,Object> rowData = new HashMap<String,Object>();
                for (int i = 1; i <= columnCount; i++) {
                    rowData.put(md.getColumnLabel(i), rs.getObject(i));
                }
                list.add(rowData);
                System.out.println(rowData);
            }


        } catch (Exception e){
            e.printStackTrace();
        } finally {
            rs.close();
            st.close();
            conn.close();
        }
    }
}
